package benny_driving_ser.action;

import java.io.IOException;
import java.io.PrintWriter;

import benny_driving_ser.dao.PayCodeDao;
import benny_driving_ser.dao.impl.PayCodeDaoImpl;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.SessionUtil;


public class PayCodeAction extends BaseAction{
	
	private PrintWriter out;
	private PayCodeDao dao_code;
	private String codesource, paytype, paycost, wait, times, dayOrNight, isActivate,payCode;
	private int state,months;
	
	public String insertPayCode(){
		
		try{
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			dao_code = new PayCodeDaoImpl();
			SessionAttributeDriId sad = SessionUtil.getInstance().verifySessionIdForDri(request);
			if(sad!=null){
				System.out.println(codesource+"--"+paytype+"--"+paycost+"--"+wait+"--"+times+"--"+dayOrNight+"--"+isActivate);
				state = dao_code.insertPayCode(codesource, paytype, paycost, wait, times, dayOrNight, isActivate);
				if(state==1){
					out.print(state);
					out.flush();
					out.close();
					return "success";
				}
				out.print(1);
				out.flush();
				out.close();
				return "fail";
			}else{
				out.print(-1);
				out.flush();
				out.close();
				return "fail";
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return "fail";
	}

	public String activatePayCode(){
		
		SessionAttributeDriId sad = SessionUtil.getInstance().verifySessionIdForDri(request);
		try{
			
			if(sad!=null){
				request.setCharacterEncoding("utf-8");
				response.setCharacterEncoding("utf-8");
				out = response.getWriter();
				dao_code = new PayCodeDaoImpl();
				System.out.println("payCode:"+payCode);
				
				if(dao_code.activatePayCode(payCode,months)==1){
					out.print(0);
					out.flush();
					out.close();
					return "success";
				}else{
					out.print(1);
					out.flush();
					out.close();
					return "fail";
				}
				
			}else{
				out = response.getWriter();
				out.print(-1);
				out.flush();
				out.close();
				return "fail";
			}
		}catch(IOException e){
			e.printStackTrace();
		}
		return "fail";
	}
	
	
	public String checkPayCode(){
		try{
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			dao_code = new PayCodeDaoImpl();
			
			state = dao_code.checkPayCode2(payCode);
			switch(state){
			case 0:
				out.print(state);
				out.flush();
				out.close();
				return "success";
			case 1:
				out.print(state);
				out.flush();
				out.close();
				return "fail";
			case 2:
				out.print(state);
				out.flush();
				out.close();
				return "fail";
			case 3:
				out.print(state);
				out.flush();
				out.close();
				return "fail";
			}
			
		}catch(Exception e){
			e.printStackTrace();
			return "fail";
		}
		return "fail";
	}
	
	
	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public PrintWriter getOut() {
		return out;
	}

	public void setOut(PrintWriter out) {
		this.out = out;
	}

	public PayCodeDao getDao_code() {
		return dao_code;
	}

	public void setDao_code(PayCodeDao dao_code) {
		this.dao_code = dao_code;
	}

	public String getCodesource() {
		return codesource;
	}

	public void setCodesource(String codesource) {
		this.codesource = codesource;
	}

	public String getPaytype() {
		return paytype;
	}

	public void setPaytype(String paytype) {
		this.paytype = paytype;
	}

	public String getPaycost() {
		return paycost;
	}

	public void setPaycost(String paycost) {
		this.paycost = paycost;
	}

	public String getWait() {
		return wait;
	}

	public void setWait(String wait) {
		this.wait = wait;
	}

	public String getTimes() {
		return times;
	}

	public void setTimes(String times) {
		this.times = times;
	}

	public String getDayOrNight() {
		return dayOrNight;
	}

	public void setDayOrNight(String dayOrNight) {
		this.dayOrNight = dayOrNight;
	}

	public String getIsActivate() {
		return isActivate;
	}

	public void setIsActivate(String isActivate) {
		this.isActivate = isActivate;
	}

	public String getPayCode() {
		return payCode;
	}

	public void setPayCode(String payCode) {
		this.payCode = payCode;
	}

	public int getMonths() {
		return months;
	}

	public void setMonths(int months) {
		this.months = months;
	}
	
	
	
//	/**
//	 * 计算支付码
//	 * @return
//	 */
//	public String calcPayCode(){
		
//		int payCost;
//		List<PayCodeBean> list = null;
//		dao_code = new PayCodeDaoImpl();
//		
//		SessionAttributeDriId sad = SessionUtil.getInstance()
//				.verifySessionIdForDri(request);
//		json = new JSONObject();
//		
//		try {
//			request.setCharacterEncoding("utf-8");
//			response.setCharacterEncoding("utf-8");
//			out = response.getWriter();
//			if(sad!=null){
//				driId = sad.getValue();
//				System.out.println("payCode:"+payCode);
//				if(dao_code.checkPayCode(payCode)==1){
//					
//					if((list = dao_code.queryPayCode(payCode))!=null){
//						System.out.println("action list:"+list.get(0).toString());
//						taximeterData = new TaximeterDataBean(period,orderid,total,sm,sop,sowt,sowp,startmil,
//								endmil, driId);
//						System.out.println("list:"+list.toString());
//						payCost = PayCodeUtil.calcCodeCost(list, taximeterData);
//						
//						taximeterData.setTotal((Integer.parseInt(total)-payCost)+"");
//						
////						dao_code.updatePayCodeState(payCode);
//						
//						json.put("state", 0+"");
//						json.put("price", payCost+"");
//						
//						out.print(json.toString());
//						out.flush();
//						out.close();
//					}else{
//						json.put("state", "1");
//						json.put("price", 0+"");
//						out.print(json.toString());
//						out.flush();
//						out.close();
//					}
//					
//				}else{
//					json.put("state", "2");
//					json.put("price", "0");
//					out.print(json.toString());
//					out.flush();
//					out.close();
//				}
//				
//				
//				
//				return "success";
//				
//				
//				
////				if(list.size()==1){
////					//先判断该支付码是否用过
////					if(Integer.parseInt((payCodeBean=list.get(0)).getUsed())==0 || Integer.parseInt(payCodeBean.getTimes())>0){
////						//判断支付码支付额度及抵扣内型 1为里程,2为价格,3为等候费用
////						if(Integer.parseInt(payCodeBean.getPayType())==1){
////							
////						}else if(Integer.parseInt(payCodeBean.getPayType())==2){
////							
////						}else if(Integer.parseInt(payCodeBean.getPayType())==3){
////							
////						}
////						
////						//计算支付额度
////					}else{
////						//该支付码已经使用,或者可使用次数为0
////						return "fail";
////					}
////					
////				}else if(list.size()>1){
////					//支付码不唯一,支付码异常
////					return "fail";
////				}
////				else{
////					//没有支付码,直接计算
////					return "success";
////				}
////				
//				
//				
//			}else{
//				json.put("-1", "0");
//				out.print(json);
//				out.flush();
//				out.close();
//				return "fail";
//			}
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (JSONException e){
//			e.printStackTrace();
//			try {
//				json.put("3", 0);
//			} catch (JSONException e1) {
//				// TODO Auto-generated catch block
//				e1.printStackTrace();
//			}
//			out.print(json);
//			out.flush();
//			out.close();
//			return "fail";
//		}
//		
//		return "fail";
//	}
//
//	public PrintWriter getOut() {
//		return out;
//	}
//
//	public void setOut(PrintWriter out) {
//		this.out = out;
//	}
//
//	public String getDriId() {
//		return driId;
//	}
//
//	public void setDriId(String driId) {
//		this.driId = driId;
//	}
//
//	public String getPayCode() {
//		return payCode;
//	}
//
//	public void setPayCode(String payCode) {
//		this.payCode = payCode;
//	}
//
//	public String getPeriod() {
//		return period;
//	}
//
//	public void setPeriod(String period) {
//		this.period = period;
//	}
//
//	public String getOrderid() {
//		return orderid;
//	}
//
//	public void setOrderid(String orderid) {
//		this.orderid = orderid;
//	}
//
//	public String getTotal() {
//		return total;
//	}
//
//	public void setTotal(String total) {
//		this.total = total;
//	}
//
//	public String getStartmil() {
//		return startmil;
//	}
//
//	public void setStartmil(String startmil) {
//		this.startmil = startmil;
//	}
//
//	public String getEndmil() {
//		return endmil;
//	}
//
//	public void setEndmil(String endmil) {
//		this.endmil = endmil;
//	}
//
//	public String getSm() {
//		return sm;
//	}
//
//	public void setSm(String sm) {
//		this.sm = sm;
//	}
//
//	public String getSop() {
//		return sop;
//	}
//
//	public void setSop(String sop) {
//		this.sop = sop;
//	}
//
//	public String getSowt() {
//		return sowt;
//	}
//
//	public void setSowt(String sowt) {
//		this.sowt = sowt;
//	}
//
//	public String getSowp() {
//		return sowp;
//	}
//
//	public void setSowp(String sowp) {
//		this.sowp = sowp;
//	}
//
//	public TaximeterDataBean getTaximeterData() {
//		return taximeterData;
//	}
//
//	public void setTaximeterData(TaximeterDataBean taximeterData) {
//		this.taximeterData = taximeterData;
//	}
//
//	public PayCodeDao getDao_code() {
//		return dao_code;
//	}
//
//	public void setDao_code(PayCodeDao dao_code) {
//		this.dao_code = dao_code;
//	}
//
//	public TaximeterDataSubmitDao getDao_submit() {
//		return dao_submit;
//	}
//
//	public void setDao_submit(TaximeterDataSubmitDao dao_submit) {
//		this.dao_submit = dao_submit;
//	}
	
}
